<template>
  <div class="trafficWrap">
    <router-view :systemInfo="systemInfo" :navItems="navItems" @getNavInfo="getNavInfo"></router-view>
  </div>

</template>
<script>
  import { Message } from 'element-ui';
  export default {
    data(){
      return {
        systemInfo: "",
        navItems: []
      }
    },
    created(){
      this.axiosSet()
      let userInfo = JSON.parse(localStorage.getItem('userInfo') ? localStorage.getItem('userInfo') : "{}")
      let btnMenuIds = JSON.parse(localStorage.getItem('getBtnMenuIds') ? localStorage.getItem('getBtnMenuIds') : "[]")
      this.$store.commit("getLoginInfo", userInfo)
      this.$store.commit("getBtnMenuIds", btnMenuIds)
      this.getCompanies()
      if (userInfo.sysUserId) {
        this.getNavInfo()
      }

//      this.doLogin()
    },
    methods: {

      axiosSet(){

        // 返回配置
        this.axios.interceptors.response.use(
          response => {  //请求成功

            const result = response.data

            if (result.code == 200) { //成功状态

            } else if (result.code == 2504 || result.code == 2512) {  //登录失效

              //清除登录成功状态
              sessionStorage.setItem('isLogin', false)
              //跳转登录页面
              setTimeout(() => {
                this.$router.push("/login")
              }, 1000)
              Message.closeAll()
              Message({
                type: 'warning',
                message: result.message
              });
            } else {
              if (result.message && result.message != "账户无权限") {
                Message.closeAll()
                Message({
                  type: 'warning',
                  message: result.message
                });
              }
            }
            return response;
          },
          error => { //失败
            if (error.response) {

            }
          });

      },
      //  登录
      doLogin(){
        if (process.env.NODE_ENV === 'development') {
          //  登录 测试
          this.axios.post(process.env.API_HOST + `/system/login`, {
            account: 'cgadmin',
            password: "12345678",
            code: "8888"
          })
            .then(response => {
              const result = response.data
              this.getCompanies()
            })

        }
      },
      //获取所有企业单位
      getCompanies(){
        this.axios.post(process.env.API_HOST + `/enterprise/selectEnterpriseList`, {
          enterpriseType: '1',
          enterpriseStatus: "0"
        })
          .then(response => {
            const result = response.data
            if (result.code == 200) {
              let companiess = []
              result.result.forEach(item => {
                companiess.push({
                  "enterpriseId": item.enterpriseId,
                  "enterpriseNo": item.enterpriseNo,
                  "enterpriseName": item.enterpriseAlias,
                  "enterpriseAlias": item.enterpriseAlias ? item.enterpriseAlias : item.enterpriseName,
                  count: 0,
                  color: item.color,
                })
              })
              this.$store.commit("getCompanies", companiess)
            }
          })
        // this.axios.post(process.env.API_HOST + `/common/selectExamineAgencyList`, {})
        //   .then(response => {
        //     const result = response.data
        //     if (result.code == 200) {
        //       this.$store.commit("getGovAgencyList", result.result)
        //     }
        //   })
      },
      getNavInfo(){
        let navItems = JSON.parse(localStorage.getItem('navItems') ? localStorage.getItem('navItems') : "[]")
        if (navItems.length > 0) {
          this.navItems = navItems
        } else {
          this.axios.post(process.env.API_HOST + `/system/selectUserMenu`, {sysUserId: this.$store.state.userInfo.sysUserId})
            .then(response => {
              const result = response.data
              if (result.code === 200) {
                this.navItems = []
                result.result.forEach((item, index) => {
                  this.navItems.push({
                    index: item.url,
                    icon: item.icon,
                    title: item.menuName,
                  })
                  if (item.childValues.length > 0) {
                    this.navItems[index].subs = []
                    item.childValues.forEach(item1 => {
                      if (item1.menuType == 2) {
                        this.navItems[index].subs.push({
                          index: item1.url,
                          title: item1.menuName
                        })
                      }
                    })
                  }
                })
                localStorage.setItem('navItems', JSON.stringify(this.navItems));
              }
            })
        }
        // this.axios.post(process.env.API_HOST + '/show/vehicleDistribution', {})
        //   .then(response => {
        //     const result = response.data
        //     if (result.code == 200) {
        //
        //     }
        //   })
      },
    },
    components: {},
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "./common/stylus/mixin.styl"
  /*@import "../static/css/font/font.css"*/
  html
    width 100%
    height 100%
    body
      position relative
      overflow-x hidden
      font-size 16px
      width 100%
      height 100%
      background-color $bg
      .trafficWrap
        overflow-x hidden
        width 100%
        height 100%
</style>


